//
//  HomeViewCell.swift
//  DKE-Album
//
//  Created by 杨东宝 on 2020/6/14.
//  Copyright © 2020 杨东宝. All rights reserved.
//

import UIKit
protocol PhotoDetailCellDelegate: AnyObject {
    //刷新
    func deleteCell(index:IndexPath)
    func refreshCell(index:IndexPath)
}
let viewRatio = (kScreenWidth - DIMENSION_VALUE(120)) / DIMENSION_VALUE(200)
class PhotoDetailCell: UITableViewCell {
    var photoInfo:PhotoInforModel?
    weak var delegate: PhotoDetailCellDelegate?
    var currentIndexPath:IndexPath!
    lazy var backView: UIView = {
        let back = UIView()
        back.backgroundColor = UIColor.white
        back.layer.cornerRadius = 10
        back.layer.masksToBounds =  true
        contentView.addSubview(back)
        return back
    }()
    
    
    //MARK:- 名称
    lazy var nameLabe: UILabel = {
        let label = UILabel()
        label.text = "NO.2"
        label.font = DIMENSION_FONT_OF_SIZE(16)
        label.textColor = HexRGB(0x3D4466)
        backView.addSubview(label)
        return label
    }()
    
    //MARK:- 选择按钮
    lazy var deleteBtn: UIButton = {
        let btn =  UIButton.init(type: .custom)
        btn.setImage(UIImage(named: "delete"), for: .normal)
        btn.addTarget(self, action: #selector(deleteBtnClick(_ : )), for: .touchUpInside)
        backView.addSubview(btn)
        return btn
    }()
  
    lazy var logoImage: UIImageView = {
        let image = UIImageView.init(image: UIImage.init(named: "dy_bg"))
        image.contentMode = .scaleAspectFit
        image.layer.cornerRadius = 5.0
        image.layer.masksToBounds = true
        image.isUserInteractionEnabled = true
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(imageTapped(_:)))
        // 将手势识别器添加到imageView
        image.addGestureRecognizer(tapGesture)
        backView.addSubview(image)
        return image
    }()
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .clear
        initSubView()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func initSubView() {
        nameLabe.snp.makeConstraints { (make) in
            make.left.equalTo(backView.snp_left).offset(DIMENSION_VALUE(5))
            make.centerY.equalTo(backView.snp_centerY)
            make.width.equalTo(DIMENSION_VALUE(60))
        }

        deleteBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(DIMENSION_VALUE(-5))
            make.centerY.equalTo(backView.snp_centerY)
            make.width.equalTo(DIMENSION_VALUE(20))
            make.height.equalTo(DIMENSION_VALUE(20))
        }
        logoImage.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabe.snp_right).offset(DIMENSION_VALUE(5))
            make.height.equalTo(DIMENSION_VALUE(80))
            make.width.equalTo(kScreenWidth - DIMENSION_VALUE(120))
            make.top.equalToSuperview().offset(DIMENSION_VALUE(10))
        }
     
        backView.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview().offset(DIMENSION_VALUE(10))
            make.right.equalToSuperview().offset(DIMENSION_VALUE(-10))
            make.bottom.equalTo(logoImage.snp_bottom).offset(DIMENSION_VALUE(10))
            make.bottom.equalToSuperview().offset(DIMENSION_VALUE(-5))
        }
    }
}
extension PhotoDetailCell{
    //MARK:-选择按钮
    @objc func deleteBtnClick(_ btn :UIButton) {
        self.delegate?.deleteCell(index: currentIndexPath)
    }
    
    @objc func imageTapped(_ gesture: UITapGestureRecognizer) {
        // 在这里处理点击事件
        self.delegate?.refreshCell(index: currentIndexPath)
    }
}
//MARK:-数据赋值
extension PhotoDetailCell{
    var model:PhotoInforModel{
        set{
            photoInfo = newValue
            self.setModel(model: photoInfo!)
        }
        get{
            return photoInfo!
        }
    }
    func setModel(model:PhotoInforModel) {
        
        let imageWith  = model.image?.size.width
        let imageHeight = model.image?.size.height

        guard let imageRatio = model.image?.aspectWH() else { return }

        
        let viewWidth = kScreenWidth - DIMENSION_VALUE(120)
        let viewHeight = DIMENSION_VALUE(200)
        
        var newWidth:CGFloat = 0
        var newHeight:CGFloat = 0
        
        if viewRatio > imageRatio  {
            newHeight = viewHeight
            newWidth = newHeight * imageRatio
        }else{
            newWidth = viewWidth
            newHeight = newWidth * CGFloat((imageHeight ?? 0.0) / (imageWith ?? 0))
        }
   
        logoImage.snp.updateConstraints { (make) in
            make.height.equalTo(newHeight)
            make.width.equalTo(newWidth)
        }
        
        logoImage.image = model.image
        nameLabe.text = "NO." +  String(describing: "\(currentIndexPath.row + 1)")
    }
}
